//
//  LineChartViewController.m
//  cocoapodText
//
//  Created by apple on 16/8/5.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ShiPei.h"
//导入时需要手动打上，系统没有提示
#import "Charts-Swift.h"
#import <Foundation/Foundation.h>
#import "LineChartViewController.h"
@interface LineChartViewController ()
@end

@implementation LineChartViewController

LineChartView *sampleLineChartView;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self initLineChartView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(void)initLineChartView{
    sampleLineChartView=[[LineChartView alloc] initWithFrame:CGRectMake(kScreenW/4, kScreenH/4, kScreenW/2, kScreenH/2)];
    sampleLineChartView.backgroundColor = [UIColor whiteColor];
    sampleLineChartView.delegate = self;
    [self.view addSubview:sampleLineChartView];
    [self initLineChartStyleAndData];
}

-(void)initLineChartStyleAndData{
    [self setupBarLineChartView:sampleLineChartView];
    [self setLineChartParam:sampleLineChartView];
    [self setLegendStyle:sampleLineChartView];
    [self initChartData];
}

- (void)setupBarLineChartView:(BarLineChartViewBase *)chartView
{
    //描述
    chartView.descriptionText = @"descriptionText";
    //无数据默认显示
    chartView.noDataTextDescription = @"You need to provide data for the chart.";
    //网格背景
    chartView.drawGridBackgroundEnabled = NO;
    chartView.dragEnabled = YES;
    [chartView setScaleEnabled:YES];
    chartView.pinchZoomEnabled = YES;
    // ChartYAxis *leftAxis = chartView.leftAxis;
    //x轴
    ChartXAxis *xAxis = chartView.xAxis;
    xAxis.labelPosition = XAxisLabelPositionBottom;
    chartView.rightAxis.enabled = NO;
}

-(void)setLineChartParam:(LineChartView *)chartView{
    // x-axis limit line
    ChartLimitLine *llXAxis = [[ChartLimitLine alloc] initWithLimit:10.0 label:@"Index 10"];
    llXAxis.lineWidth = 4.0;
    llXAxis.lineDashLengths = @[@(10.f), @(10.f), @(0.f)];
    llXAxis.labelPosition = ChartLimitLabelPositionRightBottom;
    llXAxis.valueFont = [UIFont systemFontOfSize:10.f];
    //[_chartView.xAxis addLimitLine:llXAxis];
    ChartLimitLine *ll1 = [[ChartLimitLine alloc] initWithLimit:130.0 label:@"Upper Limit"];
    ll1.lineWidth = 4.0;
    ll1.lineDashLengths = @[@5.f, @5.f];
    ll1.labelPosition = ChartLimitLabelPositionRightTop;
    ll1.valueFont = [UIFont systemFontOfSize:10.0];
    
    ChartLimitLine *ll2 = [[ChartLimitLine alloc] initWithLimit:-30.0 label:@"Lower Limit"];
    ll2.lineWidth = 4.0;
    ll2.lineDashLengths = @[@5.f, @5.f];
    ll2.labelPosition = ChartLimitLabelPositionRightBottom;
    ll2.valueFont = [UIFont systemFontOfSize:10.0];
    
    ChartYAxis *leftAxis = chartView.leftAxis;
    [leftAxis removeAllLimitLines];
    [leftAxis addLimitLine:ll1];
    [leftAxis addLimitLine:ll2];
    leftAxis.axisMaxValue = 220.0;
    leftAxis.axisMinValue = -50.0;
    leftAxis.gridLineDashLengths = @[@5.f, @5.f];
    leftAxis.drawZeroLineEnabled = NO;
    leftAxis.drawLimitLinesBehindDataEnabled = YES;
    
    chartView.rightAxis.enabled = NO;
    
    //[_chartView.viewPortHandler setMaximumScaleY: 2.f];
    //[_chartView.viewPortHandler setMaximumScaleX: 2.f];
    
    //BalloonMarker *marker = [[BalloonMarker alloc] initWithColor:[UIColor colorWithWhite:180/255. alpha:1.0] font:[UIFont systemFontOfSize:12.0] insets: UIEdgeInsetsMake(8.0, 8.0, 20.0, 8.0)];
    //marker.minimumSize = CGSizeMake(80.f, 40.f);
    //chartView.marker = marker;
    
    [chartView animateWithXAxisDuration:2.5 easingOption:ChartEasingOptionEaseInOutQuart];
}

//设置图例样式
-(void)setLegendStyle:(LineChartView *)chartView{
    
    chartView.legend.form = ChartLegendFormLine;
    //图例在柱状图中的位置
    chartView.legend.position = ChartLegendPositionBelowChartLeft;
    //图示样式: 方形、线条、圆形
    chartView.legend.form = ChartLegendFormSquare;
    //图示大小
    chartView.legend.formSize = 9.0;
    //字体大小
    chartView.legend.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:11.f];
    //间隔
    chartView.legend.xEntrySpace = 4.0;
}

- (void)initChartData
{
    [self setDataCount:44 range:100 chart:sampleLineChartView];
}

- (void)setDataCount:(int)count range:(double)range chart:(LineChartView *)chartView
{
    //x轴文字
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    for (int i = 0; i < count; i++){
        [xVals addObject:[@(i) stringValue]];
    }
    //y轴高度
    NSMutableArray *yVals = [[NSMutableArray alloc] init];
    for (int i = 0; i < count; i++){
        double mult = (range + 1);
        double val = (double) (arc4random_uniform(mult)) + 3;
        [yVals addObject:[[ChartDataEntry alloc] initWithValue:val xIndex:i]];
    }
    
    LineChartDataSet *set1 = nil;
    if (chartView.data.dataSetCount > 0){
        set1 = (LineChartDataSet *)chartView.data.dataSets[0];
        set1.yVals = yVals;
        chartView.data.xValsObjc = xVals;
        [chartView.data notifyDataChanged];
        [chartView notifyDataSetChanged];
    }else{
        set1 = [[LineChartDataSet alloc] initWithYVals:yVals label:@"DataSet 1"];
        
        set1.lineDashLengths = @[@5.f, @2.5f];
        set1.highlightLineDashLengths = @[@5.f, @2.5f];
        [set1 setColor:UIColor.blackColor];
        [set1 setCircleColor:UIColor.blackColor];
        set1.lineWidth = 1.0;
        set1.circleRadius = 3.0;
        set1.drawCircleHoleEnabled = NO;
        set1.valueFont = [UIFont systemFontOfSize:9.f];
        //set1.fillAlpha = 65/255.0;
        //set1.fillColor = UIColor.blackColor;
        
        NSArray *gradientColors = @[
                                    (id)[ChartColorTemplates colorFromString:@"#00ff0000"].CGColor,
                                    (id)[ChartColorTemplates colorFromString:@"#ffff0000"].CGColor
                                    ];
        CGGradientRef gradient = CGGradientCreateWithColors(nil, (CFArrayRef)gradientColors, nil);
        
        set1.fillAlpha = 1.f;
        set1.fill = [ChartFill fillWithLinearGradient:gradient angle:90.f];
        set1.drawFilledEnabled = YES;
        
        CGGradientRelease(gradient);
        
        NSMutableArray *dataSets = [[NSMutableArray alloc] init];
        [dataSets addObject:set1];
        
        LineChartData *data = [[LineChartData alloc] initWithXVals:xVals dataSets:dataSets];
        chartView.data = data;
    }
}


#pragma mark - ChartViewDelegate

- (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry dataSetIndex:(NSInteger)dataSetIndex highlight:(ChartHighlight * __nonnull)highlight
{
    NSLog(@"chartValueSelected");
}

- (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
{
    NSLog(@"chartValueNothingSelected");
}

@end